/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.SchoStutype;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.SchoStuwork;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.StuJob;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.SchoStutypeService;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.SchoStuworkService;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.StuJobService;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 勤工业务Controller
 * @author zms
 * @version 2018-03-23
 */
@Controller
@RequestMapping(value = "/api/schoolSource/stujob")
public class StujobApiController extends BaseController {
	@Autowired
	private SchoStuworkService schoStuworkService;
	@Autowired
	private StuJobService stuJobService;
	@RequestMapping(value = "jobList")
	@ResponseBody   //获取勤工列表
	public Map<String, Object> getNotice(SchoStuwork schoStuwork, HttpServletRequest request, HttpServletResponse response) {
		schoStuwork.setReqStates("1");
		Page<SchoStuwork> page = schoStuworkService.findPage(new Page<SchoStuwork>(request, response),  schoStuwork);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage", page.getTotalPage());
		return map;
	}
	@RequestMapping(value = "enlist")
	@ResponseBody  //勤工报名
	public Map<String, Object> getNotice(StuJob stuJob, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal(); //用户是否掉线
		if(principal!=null) {
			stuJob.setStuId(principal.toString());//插入的报名者id
			List<StuJob> stuJobs = stuJobService.findList(stuJob); //判断报名信息是否重复
			if (stuJobs.isEmpty()) {
				if(StringUtils.isNotBlank(stuJob.getJobId())) {
					stuJobService.save(stuJob);
					map.put("success", true);
					map.put("message","报名成功");
					return map;
				} else {
					map.put("message","勤工报名id没有上传");
					map.put("success", false);
					return map;
				}
			} else {
				map.put("message", "用户已经报名");
				map.put("success", false);
				return map;
			}
		} else {
			map.put("message","用户掉线了");
			map.put("success", false);
			return map;
		}
	}
	@RequestMapping(value = "findById")
	@ResponseBody //勤工信息详情
	public Map<String, Object> findById(SchoStuwork schoStuwork, HttpServletRequest request, HttpServletResponse response) {
		SchoStuwork getschoStuwork =schoStuworkService.get(schoStuwork);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		map.put("page",getschoStuwork);
		return map;
	}
}